<?php
// +----------------------------------------------------------------------
// | Yzncms [ 御宅男工作室 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2007 http://yzncms.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: fastadmin: https://www.fastadmin.net/
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\CarModel;
use app\admin\model\College;
use app\admin\model\UserModel;
use app\common\controller\Adminbase;
use app\common\model\AliPay;
use app\common\model\FishLoc;
use app\common\model\LocVideo;
use app\common\model\MemberModel;
use app\common\model\RebateLog;
use util\Tree;


class Loc extends Adminbase
{

    protected function initialize()
    {
        parent::initialize();
        $this->modelClass = new FishLoc();
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($page, $limit, $where, $sort, $order) = $this->buildTableParames();
            $list = $this->modelClass->with('user')
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);
            $date = date('Y-m-d');
            $yesterday = date('Y-m-d', strtotime('-1 day'));
            foreach ($list as $key => &$value) {
                $rebate = (new RebateLog())->where(['loc_id' => $value['id'], 'day' => $date])->field('count(id) as vide_num')->find();
                $yesterdayRebate = (new RebateLog())->where(['loc_id' => $value['id'], 'day' => $yesterday])->field('count(id) as vide_num')->find();
                $value['day_video'] = $rebate['vide_num'];
                $value['yesterday_video'] = $yesterdayRebate['vide_num'];
            }
            $result = ["code" => 0, 'count' => $list->total(), "data" => $list->items()];
            return json($result);
        }
        return $this->fetch();
    }


    public function check()
    {
        $id = $this->request->param('id/d', 0);
        $row = $this->modelClass->get($id);
        if (!$row) {
            $this->error('记录未找到');
        }
        if ($this->request->isPost()) {
            $this->token();
            $params = $this->request->post('');
            if ($params['status'] == 3 && empty($params['reason'])) {
                return $this->error('请填写不通过的理由');
            }

            try {
                $row->allowField(true)->save($params);
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
            $this->success("修改成功！");
        }

        $this->assign("data", $row);
        return $this->fetch();
    }


    public function edit()
    {
        $id = $this->request->param('id/d', 0);
        $loc = $this->modelClass
            ->with(['user'])
            ->where('yzn_fish_loc.id', $id)
            ->find();

        if ($this->request->isAjax()) {

            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($page, $limit, $where, $sort, $order) = $this->buildTableParames();

            $list = RebateLog::
            where($where)
                ->where(['loc_id' => $id])
                ->order('create_time desc')
                ->paginate($limit);

            $result = ["code" => 0, 'count' => $list->total(), "data" => $list->items()];
            return json($result);
        }

        $date = date('Y-m-d');
        $yesterday = date('Y-m-d', strtotime('-1 day'));

        $rebate = (new RebateLog())->where(['loc_id' => $id, 'day' => $date])->field('count(id) as vide_num')->find();
        $yesterdayRebate = (new RebateLog())->where(['loc_id' => $id, 'day' => $yesterday])->field('count(id) as vide_num')->find();
        $loc['day_video'] = $rebate['vide_num'];
        $loc['yesterday_video'] = $yesterdayRebate['vide_num'];


        $this->assign('data', $loc);
        return $this->fetch();
    }


}
